/*
 * ProjetoPSA - Plano de Substituição de Aulas
 *    Copyright (C) 2010  Marcelo e Cicero
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetoPSA.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Marcelo e Cicero
 */
public class DateUtils {

    public Date string4Date(String stringData) throws ParseException, Exception {
        return string4Date(stringData, EnumDateFormat.DATA_TIME);
    }

    public Date string4Date(String stringData, EnumDateFormat formato) throws ParseException, Exception {
//        if (!validaData(stringData)) {
//            throw new Exception("Data invalida!");
//        }
        return new SimpleDateFormat(formato.getFormat()).parse(stringData);
    }

    public String date4String(Date data) {
        return date4String(data, EnumDateFormat.DATA);
    }

    public String date4String(Date data, EnumDateFormat formato) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(formato.getFormat());
        StringBuilder dateString = new StringBuilder(dateFormat.format(data));

        return dateString.toString();
    }

    public static boolean validaData(String data) throws ParseException {
        Boolean ret = false;
        String DatePattern = "^(?:(31)(\\D)(0?[13578]|1[02])\\2|(29|30)(\\D)(0?[13-9]|1[0-2])\\5|(0?[1-9]|1\\d|2[0-8])(\\D)(0?[1-9]|1[0-2])\\8)((?:1[6-9]|[2-9]\\d)?\\d{2})$|^(29)(\\D)(0?2)\\12((?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:16|[2468][048]|[3579][26])00)$";
        if (data.matches(DatePattern)) {
            ret = true;
        } else {
            ret = false;
        }
        return ret;
    }
}
